﻿using aier.business.logic;
using log4net;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 测试API
    ///OData http://localhost:9000/api/Sample/FilterData?$top=1&$orderby=id%20desc
    /// </summary>

    [RoutePrefix("api/Simple")]
    public class SimpleController : ApiController
    {
        private readonly ILog log = null;
        private readonly HomeBll homeBll = null;

        /// <summary>
        /// 构成函数注入
        /// </summary>
        /// <param name="log"></param>
        /// <param name="homeBll"></param>
        public SimpleController(ILog log, HomeBll homeBll)
        {
            this.log = log;
            this.homeBll = homeBll;
        }




        /// <summary>
        /// OData 条件筛选 
        /// </summary>
        /// <param name="queryOptions"></param>
        /// <returns></returns>
        [Authorize(Roles ="read")]
        [HttpGet]
        [ActionName("FilterData")]
        public async Task<IHttpActionResult> FilterData(ODataQueryOptions<Poco> queryOptions)
        {
            try
            {
                var data = new Poco[] {
                                new Poco() { id = 1, name = "one", type = "a" },
                                new Poco() { id = 2, name = "two", type = "b" },
                                new Poco() { id = 3, name = "three", type = "c" }
                            };

                return await Task.Factory.StartNew<OkNegotiatedContentResult<Poco[]>>(() =>
                {
                    var results = data;
                    if (queryOptions.Skip != null)
                        results = results.Skip(queryOptions.Skip.Value).ToArray();
                    if (queryOptions.Top != null)
                        results = results.Take(queryOptions.Top.Value).ToArray();
                    return Ok(results);
                });
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }


        /// <summary>
        /// Odata分页测试
        /// </summary>
        /// <param name="queryOptions"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [ActionName("GetPage")]
        public PageResult<Poco> GetPage(ODataQueryOptions<Poco> queryOptions)
        {
            ODataQuerySettings oDataQuerySettings = new ODataQuerySettings()
            {
                PageSize = 5
            };



            var data = new Poco[] {
                                new Poco() { id = 1, name = "one", type = "a" },
                                new Poco() { id = 2, name = "two", type = "b" },
                                new Poco() { id = 3, name = "three", type = "c" },
                                new Poco() { id = 4, name = "three", type = "c" }
                            };

            return new PageResult<Poco>(
                    data as IEnumerable<Poco>,
                    Request.GetNextPageLink(oDataQuerySettings.PageSize.Value),
                    Request.ODataProperties().TotalCount);
        }
    }

    public class Poco
    {
        public int id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
    }
}
